<?php
class ImageCutUtil{

    public $cut = array();

    public $param = array();

    public $image;

    public function cut($source, $target, $resetColor = false, $maxWidth){
        $return = $this->init($source, $target);
        if( $return <= 0 ){
            return;
        }
        $this->image = $this->loadsource();
        $this->cutinfo();
        $newImage = imagecreatetruecolor($this->cut['width'],$this->cut['height']);
        //随机更改图片颜色
        if( $resetColor == true ){
            $color = $this->getRgb();
            $rgb1 = intval($color[0]);
            $rgb2 = intval($color[1]);
            $rgb3 = intval($color[2]);
            imagefilter($this->image, IMG_FILTER_COLORIZE, $rgb1, $rgb2, $rgb3);
        }
        imagecopyresampled($newImage, $this->image, 0, 0, $this->cut['left'], $this->cut['top'], $this->cut['width'], $this->cut['height'], $this->cut['width'], $this->cut['height']);
        //等比缩放
        if( $maxWidth > 0 ) {
            $rate = $maxWidth / $this->cut['width']; //计算绽放比例
            $maxHeight = floor($this->cut['height'] * $rate); //计算出缩放后的高度
            $newImage2 = imagecreatetruecolor($maxWidth, $maxHeight); //创建一个缩放的画布
            imagecopyresized($newImage2,$newImage, 0, 0, 0, 0, $maxWidth, $maxHeight, $this->cut['width'], $this->cut['height']); //缩放
            $imagefunc = &$this->imagefunc;
            @$imagefunc($newImage2, $this->target);
            imagedestroy($newImage);
            imagedestroy($newImage2);
        }else{
            $imagefunc = &$this->imagefunc;
            @$imagefunc($newImage, $this->target);
            imagedestroy($newImage);
        }
    }

    /**
     * 图片裁剪，取出纯白底部
     */
    protected function cutinfo(){
        //top去边、去除上边框
        $this->cut['top'] = 0;
        for($i = 0; $i < $this->imginfo['height']; ++$i){//高
            $temp = 0;
            for( $j = 0; $j < $this->imginfo['width']; ++$j){//宽
                $rgb = imagecolorat($this->image,$j,$i);
                $rgbarray = imagecolorsforindex($this->image, $rgb); //获取图片RGB数组
                if( $rgbarray['red'] > 250 || $rgbarray['green'] > 250 || $rgbarray['blue'] > 250 ){//白色像素
                }else{//非白色像素
                    ++$temp;
                }
            }
            if ($temp==0){
                ++$this->cut['top'];
            }else{
                break;
            }
        }
        //bottom去边、去除下边框
        $this->cut['bottom'] = 0;
        for( $i = $this->imginfo['height']-1; $i > 0;--$i){//高
            $temp = 0;
            for( $j = 0; $j < $this->imginfo['width']; ++$j){//宽
                $rgb = imagecolorat($this->image, $j, $i);
                $rgbarray = imagecolorsforindex($this->image, $rgb); //获取图片RGB数组
                if($rgbarray['red']>250 || $rgbarray['green']>250 || $rgbarray['blue']>250){//白色像素
                }else{//非白色像素
                    ++$temp;
                }
            }
            if ($temp==0){
                ++$this->cut['bottom'];
            }else{
                break;
            }
        }
        //left去边、去除左边框
        $this->cut['left'] = 0;
        for( $j = 0; $j < $this->imginfo['width']; ++$j){//宽
            $temp = 0;
            for( $i = 0; $i < $this->imginfo['height']; ++$i){//高
                $rgb = imagecolorat($this->image,$j,$i);
                $rgbarray = imagecolorsforindex($this->image, $rgb); //获取图片RGB数组
                if($rgbarray['red']>250 || $rgbarray['green']>250 || $rgbarray['blue']>250){//白色像素
                }else{//非白色像素
                    ++$temp;
                }
            }
            if ($temp==0){
                ++$this->cut['left'];
            }else{
                break;
            }
        }
        //right去边、去除右边框
        $this->cut['right']=0;
        for($j = $this->imginfo['width']-1; $j >0 ;--$j){//宽
            $temp = 0;
            for($i = 0; $i < $this->imginfo['height']; ++$i){//高
                $rgb = imagecolorat($this->image,$j,$i);
                $rgbarray = imagecolorsforindex($this->image, $rgb); //获取图片RGB数组
                if($rgbarray['red']>250 || $rgbarray['green']>250 || $rgbarray['blue']>250){//白色像素
                }else{//非白色像素
                    ++$temp;
                }
            }
            if ($temp==0){
                ++$this->cut['right'];
            }else{
                break;
            }
        }
        $this->cut['width'] = $this->imginfo['width']-$this->cut['left']-$this->cut['right'];
        $this->cut['height'] = $this->imginfo['height']-$this->cut['top']-$this->cut['bottom'];
    }

    /**
     * 加载图片资源
     */
    protected function loadsource() {
        //加载图片资源
        $imagecreatefromfunc = &$this->imagecreatefromfunc;
        $im = @$imagecreatefromfunc($this->source);
        if(!$im) {
            if(!function_exists('imagecreatefromstring')) {
                return -4;
            }
            $fp = @fopen($this->source, 'rb');
            $contents = @fread($fp, filesize($this->source));
            fclose($fp);
            $im = @imagecreatefromstring($contents);
            if($im == FALSE) {
                return -1;
            }
        }
        return $im;
    }

    /**
     * 图片初始化，原图和目标图片
     */
    protected function init($source,$target){
        $this->imageinfo = @getimagesize($source);
        $this->source = $source;
        $this->target = $target;
        $this->imginfo['width'] = $this->imageinfo[0];
        $this->imginfo['height'] = $this->imageinfo[1];
        $this->imginfo['mime'] = $this->imageinfo['mime'];
        $this->imginfo['size'] = @filesize($source);
        $this->libmethod = isset($this->param['imagelib']) && isset($this->param['imageimpath']);
        if(!$this->libmethod) {
            switch($this->imginfo['mime']) {
                case 'image/jpeg':
                    $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
                    $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
                    break;
                case 'image/gif':
                    $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
                    $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
                    break;
                case 'image/png':
                    $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
                    $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
                    break;
            }
        } else {
            $this->imagecreatefromfunc = $this->imagefunc = TRUE;
        }
        if(!$this->libmethod && $this->imginfo['mime'] == 'image/gif') {
            if(!$this->imagecreatefromfunc) {
                return -4;
            }
            if(!($fp = @fopen($source, 'rb'))) {
                return -2;
            }
            $content = fread($fp, $this->imginfo['size']);
            fclose($fp);
            $this->imginfo['animated'] = strpos($content, 'NETSCAPE2.0') === FALSE ? 0 : 1;
        }
        return $this->imagecreatefromfunc ? 1 : 0;
    }

    /**
     * 随机获取RGB颜色值
     * @return string
     */
    protected function getRgb(){
        $source = [
            "138 49 221",
            "42 152 221",
            "92 169 40",
            "115 221 173",
            "221 31 148",
            "57 226 51",
            "255 131 26",
            "2 80 49",
            "221 171 53",
            "221 0 103",
            "93 42 221",
            "221 20 28",
            "221 112 57"
        ];
        $key = array_rand($source,1);
        $rgb = $source[$key];
        $color = explode(" ",$rgb);
        return $color;
    }
}
?>
